
import java.util.ArrayList;
import org.jfree.ui.RefineryUtilities;


public class FrameStatistic extends javax.swing.JFrame 
{
    private ArrayList<String> columns_header = new ArrayList<String>();
    private MyTableModel model;
    private StatisticalData statistical_data = new StatisticalData(); 
    
    public FrameStatistic() 
    {
        initComponents();
        createColumnsHeader();
        model = new MyTableModel(columns_header);
        Table.setModel(model);
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        Table = new javax.swing.JTable();
        label_reliability = new javax.swing.JLabel();
        combo_box_reliability = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        Table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        Table.setEnabled(false);
        Table.setRowHeight(25);
        jScrollPane1.setViewportView(Table);

        label_reliability.setText("Reliability (P): ");

        combo_box_reliability.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0.70", "0.75", "0.80", "0.85", "0.90", "0.95", "0.99" }));
        combo_box_reliability.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_box_reliabilityActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 592, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(label_reliability)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(combo_box_reliability, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(label_reliability)
                    .addComponent(combo_box_reliability, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void combo_box_reliabilityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_box_reliabilityActionPerformed
    double reliability = Double.parseDouble(combo_box_reliability.getSelectedItem().toString());
    updateStyudentsCoefficient(reliability);
    updateConfidenceInterval();
    updateRelativeMistake();
}//GEN-LAST:event_combo_box_reliabilityActionPerformed

    public void runStatistic()
    {
        java.awt.EventQueue.invokeLater(new Runnable() 
        {
            public void run() 
            {
                initFrame();
            }
        });
    }
    private void initFrame()
    {
        setTitle("Statistic");    
        RefineryUtilities.centerFrameOnScreen(this);
        setVisible(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);  
        
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable Table;
    private javax.swing.JComboBox combo_box_reliability;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel label_reliability;
    // End of variables declaration//GEN-END:variables

    public void addRowToTable(ArrayList<Object> values)
    {
        model.addRow(values.toArray());
    }
    public void createColumnsHeader()
    {
        columns_header.add("№");
        columns_header.add("n");
        columns_header.add("M(X)");
        columns_header.add("sigma(X)");
        columns_header.add("L");
        columns_header.add("delta X");
        columns_header.add("E, %");
        columns_header.add("line");
    }
    private void updateStyudentsCoefficient(Double reliability)
    {
        int row_count = model.getRowCount();
        int degrees_freedom_count = (Integer)model.getValueAt(1, 1);           
        Double new_styudents_coefficient = statistical_data.getStyudentsCoefficient(reliability, degrees_freedom_count);
        for(int i = 0; i < row_count; i++)
        {
            model.setValueAt(new_styudents_coefficient, i, 4);
        }
    }
    private void updateConfidenceInterval()
    {
        int row_count = model.getRowCount();
        double styudents_coefficient = (Double)model.getValueAt(1, 4);
        for(int i = 0; i < row_count; i++)
        {
            double average_sigma = (Double)model.getValueAt(i, 3);
            double confidence_interval = statistical_data.getConfidenceInterval(styudents_coefficient, average_sigma);
            model.setValueAt(confidence_interval, i, 5);
        }
    }
    private void updateRelativeMistake()
    {
        double row_count = model.getRowCount();
        for(int i = 0; i < row_count; i++)
        {
            double mathematical_expectation = (Double)model.getValueAt(i, 2);
            double confidence_interval = (Double)model.getValueAt(i, 5);
            double relativeMistake = statistical_data.getRelativeMistake(mathematical_expectation, confidence_interval);
            model.setValueAt(relativeMistake, i, 6);
        }
    }
}
